Math.random()的用法

您所在的位置:网站首页 random 函数怎么用 Math.random()的用法

Math.random()的用法

2024-07-14 20:23| 来源: 网络整理| 查看: 265

Math.random( ) 的使用

1.介绍:

Math.random()是令系统随机选取大于等于 0.0 且小于 1.0 的伪随机 double 值

2.使用方法:

例:

int num = (int)(Math.random()*2+1) //以上代码即设置一个随机1到3(取不到3)的变量num。

例:  【应用场景描述:】  用Math类的random()方法产生一个字符,若该字符是一个大写英文字母,则输入 “Yes!”,否则输出“NO”。  random()方法产生的随机数在0.0和1.0之间,乘以128后,其值在0.0和128.0之间,将它转换为char类型后,用if来判断是否在’A’ 和’Z’之间。

public class IsUpper{ public static void main(String [] args){ char ch; ch = ( char ) ( Math.random() * 128); if ( ch >= 'A' && ch


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3